<?php

namespace App\Http\Controllers\home;

// use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
use App\Http\Requests;
use Illuminate\Support\Facades\Cookie;
use Request;

class CheckoutController extends Controller
{
    //
    public function index()
    {
        $county = DB::table("city") -> where("parent_id","0") -> get();//查询省份

        $user_name = Cookie::get('u_name');
        $user = DB::table('user') -> where("user_name",$user_name) -> first();
        $user_id = $user -> user_id;
        
        // DB::connection()->enableQueryLog();
        $shopcart = DB::table('shopcar as car')
                    -> leftJoin("good as g","g.good_id","car.good_id")
                    -> leftJoin("goodcolor as gc","gc.goodcolor_id","car.goodcolor_id")
                    -> leftJoin("goodsize as gz","gz.goodsize_id","car.goodsize_id")
                    -> where("user_id",$user_id) 
                    -> get();
        // dump(DB::getQueryLOg());die;
        // dd($shopcart);
        $total = 0;//订单总价
        foreach($shopcart as $k => $v){
            $good_price = $v -> good_price;
            $number = $v -> number;
            $good_price1 = $good_price * $number;
            $shopcart[$k]->good_price1 = $good_price1;

            $total += $good_price1;
        }

        return view('home/checkout',compact("county","shopcart","total","user"));
    }

    //城市
    public function city()
    {
        $input = Request::all();

        $county_id = $input['county_id'];//县/直辖市的county_id
        $city = DB::table("city") -> where("parent_id",$county_id) -> get();
        $str='<option value="">选择城市</option>';
		foreach($city as $v){
			$str.='<option value="'.$v->city_id.'">'.$v->city_name.'</option>';
		}
        echo $str;exit();
    }

    //乡镇
    public function town()
    {
        $input = Request::all();

        $city_id = $input['city_id'];//县/直辖市的city_id
        $town = DB::table("city") -> where("parent_id",$city_id) -> get();
        $str='<option value="">选择城市</option>';
		foreach($town as $v){
			$str.='<option value="'.$v->city_id.'">'.$v->city_name.'</option>';
		}
        echo $str;exit();
    }
}
